package org.bronycorps.mlptss.domain.actor;

import org.bronycorps.mlptss.domain.controller.AbstractController;
import org.bronycorps.mlptss.domain.controller.BossController;
import org.bronycorps.mlptss.domain.model.AbstractActorModel;
import org.bronycorps.mlptss.domain.model.event.StateChangeEvent;
import org.bronycorps.mlptss.domain.model.event.StateChangeEventListener;
import org.bronycorps.mlptss.manager.TrajectFactory;
import org.bronycorps.mlptss.utils.DrawableAnimation;

import com.badlogic.gdx.math.Vector2;

public class BossActor extends AbstractActor implements
		StateChangeEventListener {

	private BossController bossController;

	public BossActor(BossController controller) {
		super();
		this.bossController = controller;
		controller.getModel().addListener(this);

		createAnimation();

		setDrawable(this.currentAnimation.getDrawableKeyFrame(this.currentTime));
		setPosition(getModelPosition().x, getModelPosition().y);
		setWidth(getPrefWidth());
		setHeight(getPrefHeight());
		setOrigin(getModelOrigin().x, getModelOrigin().y);

		this.hitBox.setWidth(getBounds().width);
		this.hitBox.setHeight(getBounds().height);
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		switch (this.bossController.getModel().getState()) {
		case EXPLODE:
			if (this.currentAnimation.isAnimationFinished(this.currentTime)) {
				terminate();
			}
			break;
		default:
			break;
		}
	}

	private void createAnimation() {
		this.currentAnimation = getDefaultAnimation();
	}

	public BossController getBossController() {
		return this.bossController;
	}

	@Override
	protected AbstractController getController() {
		return this.bossController;
	}

	private DrawableAnimation getDefaultAnimation() {
		return this.bossController.getModel().getDefaultAnimation();
	}

	@Override
	public AbstractActorModel getModel() {
		return this.bossController.getModel();
	}

	@Override
	protected Vector2 getModelPosition() {
		return this.bossController.getModel().getPosition();
	}

	@Override
	public void onStateChange(StateChangeEvent event) {
		switch (event.getBoss().getState()) {
		case EXPLODE:
			// when exploding, the entity must not move anymore
			TrajectFactory.getInstance().clearTweens(this);
			// switch to explosion animation
			break;
		default:
			switchAnimation(getDefaultAnimation());
			break;
		}
	}

	public void terminate() {
		getTrack().removeBoss(this);
	}

}
